//
//  UIView+ABCRepeateClick.m
//  ABCRepeateClick
//
//  Created by luoshizhen on 2024/7/26.
//

#import "UIView+ABCRepeateClick.h"
#import <objc/runtime.h>


NSString * const abcPreventRepeateClickKey = @"abcPreventRepeateClickKey";

@implementation UIView (ABCRepeateClick)

- (void)setAbcPreventRepeateClick:(BOOL)abcPreventRepeateClick{
    objc_setAssociatedObject(self, (__bridge const void *)(abcPreventRepeateClickKey), @(abcPreventRepeateClick), OBJC_ASSOCIATION_ASSIGN);
}
- (BOOL)abcPreventRepeateClick{
    NSNumber *value = objc_getAssociatedObject(self, (__bridge const void *)(abcPreventRepeateClickKey));
    return value.boolValue;
}

- (BOOL)abcPreventRepeateClickAction:(NSTimeInterval)delay
{
    if (self.abcPreventRepeateClick == true) {
        return false;
    }
    self.abcPreventRepeateClick = true;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.abcPreventRepeateClick = false;
    });
    return true;
}
- (void)abcPreventRepeateClickAction:(NSTimeInterval)delay actionBlock:(abcViewClick)clickBlock
{
    if (self.abcPreventRepeateClick == true) {
        return;
    }
    self.abcPreventRepeateClick = true;
    if (clickBlock != nil) {
        clickBlock();
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.abcPreventRepeateClick = false;
    });
  
}
@end
